package com.evamay.kotlindemo.ui.main

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView

import com.evamay.kotlindemo.R
import com.evamay.kotlindemo.entity.TypeInfo

class MainAdapter( var context: Context,  var list: List<TypeInfo>?) : RecyclerView.Adapter<MainAdapter.MainViewHolder>() {
    private var clickListener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_mian, parent, false)
        return MainViewHolder(view)
    }

    override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
        holder.btn_menu.text = list!![position].menu
        holder.btn_menu.setOnClickListener {
            if (clickListener != null) {
                clickListener!!.onItemClick(position)
            }
        }
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setClickListener(clickListener: OnItemClickListener) {
        this.clickListener = clickListener
    }

    override fun getItemCount(): Int {
        return if (list == null) 0 else list!!.size
    }

    inner class MainViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var btn_menu: Button

        init {
            btn_menu = itemView.findViewById(R.id.btn_menu)
        }
    }
}
